// pages/Index/shopActive/invitation/addInvitation/addInvitation.js
const controller = require('../../../../controllers/controllerIndex').controller

var dateTimePicker = require('../../../../../utils/dateTimePicker.js');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList: [],
    address: "",
    context: "",
    invitee: "",
    timeListArray: '',
    timeListNum: '',
    time: '',
    giveType:0

  },

  onLoad: function (options) {
    this.getDateTimeData()

  },
  onShow: function () {

  },
  // 获取时间列表数据
  getDateTimeData() {
    const startYear = 2010
    const endYear = 2050
    // 获取完整的年月日 时分秒，以及默认显示的数组
    let timeObj = dateTimePicker.dateTimePicker(startYear, endYear);
    // 精确到时的处理，将数组的秒去掉
    let timeListArray = timeObj.dateTimeArray;
    let timeListNum = timeObj.dateTime;

    timeListArray.splice(timeListArray.length - 1, 1);
    timeListNum.splice(timeListNum.length - 1, 1);

    this.setData({
      timeListNum,
      timeListArray,
    });
  },

  //选择时间
  bindTime(e) {
    let timeListArray = this.data.timeListArray
    let timeListNum = this.data.timeListNum
    // 避免大小月切换日选项错误
    if (timeListNum[2] > timeListArray[2].length - 1) timeListNum[2] = 0
    let time = timeListArray[0][timeListNum[0]] + '-' + timeListArray[1][timeListNum[1]] + '-' + timeListArray[2][timeListNum[2]] + ' ' + timeListArray[3][timeListNum[3]] + ':' + timeListArray[4][timeListNum[4]]
    this.setData({
      timeListNum: e.detail.value,
      time
    })
  },
  changeTimeColumn(e) {
    var arr = this.data.timeListNum,
      dateArr = this.data.timeListArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      timeListArray: dateArr,
      timeListNum: arr
    });
  },

  inputAddress(e) {
    let address = e.detail.value
    this.setData({
      address
    })
    return address
  },
  inputContext(e) {
    let context = e.detail.value
    this.setData({
      context
    })
    return context
  },
  inputInvitee(e) {
    let invitee = e.detail.value
    this.setData({
      invitee
    })
    return invitee
  },
  //添加图片
  addImg() {
    let that = this;
    let imgList = this.data.imgList;
    wx.chooseImage({
      success(res) {
        // console.log(res);
        const tempFiles = res.tempFiles;
        const isAdd = that.judgeTotal(tempFiles[0].size)
        if (!isAdd) return
        wx.uploadFile({
          url: 'https://www.xlshhr.com/beauty-industry/api/img/uploadFile',
          filePath: tempFiles[0].path,
          name: 'image',
          formData: {
            'sessionId': wx.getStorageSync('token')
          },
          success(res) {
            // console.log('123', res);
            let src = JSON.parse(res.data)
            imgList.push(src.data);
            that.setData({
              imgList
            })
          }
        })
      }
    })
  },
  judgeTotal(size) {
    let total = this.data.total
    total += size
    if (total > 200 * 1024 * 1024) {
      wx.showToast({
        title: '添加失败，超过200M上限',
        icon: 'none'
      })
      return false
    }
    this.setData({
      total
    })
    return true
  },
  delete() {
    wx.navigateBack()
  },
  previewImg(e) {
    const index = e.target.dataset.index
    const imgList = this.data.imgList
    wx.previewImage({
      current: imgList[index],
      urls: imgList,
    })
  },
  selectForward(e) {
    this.setData({
      giveType:e.currentTarget.dataset.givetype
    })
  },
  save() {
    let value = {
      context: this.data.context,
      agreeTime: this.data.time,
      nick: this.data.invitee,
      address: this.data.address,
      image: this.data.imgList.join(','),
      shopId: wx.getStorageSync('shopId'),
      sessionId: wx.getStorageSync('token'),
      giveType:Number(this.data.giveType)
    }
    controller.invitationSave(value).then(res => {
      wx.navigateBack()
    })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})